<?php
/**
 * Кдасс единицы поискового запроса
 *
 */
class Search_ResultItem implements ArrayAccess {
	/**
	 * Данные
	 *
	 * @var array
	 */
	private $_data;

	/**
	 * Конструктор
	 *
	 * @param array $item
	 * @param int $index
	 */
	public function __construct($item, $index) {
		if (0 === (int) $item['error'] && (@$item["total"]) > 0) {
			$this->_data['ids'] = array();
			$sIndexes = Search_Indexes::getInstance();
			$id = $sIndexes->getIndex($index) . '_id'; // нафига?

			$this->_data['total'] = $item['total_found'];
			$this->_data['index'] = $sIndexes->getIndex($index);
			$this->_data['model'] = $sIndexes->getIndexModel($sIndexes->getIndex($index));
			$this->_data['model_id'] = $sIndexes->getModelId($sIndexes->getIndex($index));
			$this->_data['fields'] = $item['fields'];
			
			$this->_data['attrs'] = $item['attrs'];


			if (isset($item['words']) && is_array(@$item['words'])) {
				$this->_data['words'] = implode(' ', array_keys($item['words']));
			} else {
				$this->_data['words'] = '';
			}

			if (isset($item['matches']) && is_array(@$item['matches'])) {
				$this->_data['matches'] = $item['matches'];
				foreach ($item['matches'] as $match) {
					array_push($this->_data['ids'], $match['id']);
				}
			} else {
				$this->_data['total'] = 0;
			}
		} else {
			$this->_data['total'] = 0;
		}
	}

	/**
	 * @see интерфейс ArrayAccess
	 *
	 */
	public function offsetSet($offset, $value) {
		$this->_data[$offset] = $value;
	}

	/**
	 * @see интерфейс ArrayAccess
	 *
	 */
	public function offsetExists($offset) {
		return isset($this->_data[$offset]);
	}

	/**
	 * @see интерфейс ArrayAccess
	 *
	 */
	public function offsetUnset($offset) {
		unset($this->_data[$offset]);
	}

	/**
	 * @see интерфейс ArrayAccess
	 *
	 */
	public function offsetGet($offset) {
		return isset($this->_data[$offset]) ? $this->_data[$offset] : null;
	}
}